from app.outdoor.models import Activity
from app.aoduo_admin.models import AdminOperationLog
from django.http import JsonResponse
from rest_framework.views import APIView
from common.authCheck.adminAuth import check_admin_login, getAdminUser, adminAuthCheck
from common.dictInfo import model_to_dict
import json


class AdminActivityControlView(APIView):
    """
    id5: ADD Activity
    id6: DELETE Activity
    id7: VIEW Activity
    id8: MOD Activity
    """

    @check_admin_login
    def get(self, request, acid):
        """
        查看活动详细信息
        :param request:
        :param acid:
        :return:
        """
        try:
            admin_user = getAdminUser(request.session.get('admin'))
            authID = 7
            if adminAuthCheck(admin_user, authID):
                activity = Activity.objects.filter(id=acid)
                if not activity.exists():
                    return JsonResponse({
                        'status': False,
                        'errMsg': '活动不存在'
                    }, status=404)
                activity = activity[0]
                activityDict = model_to_dict(activity)
                AdminOperationLog.objects.create(
                    related_admin=admin_user,
                    related_auth_type_id=authID,
                    other_message='view activity ' + str(activity.id) + ' ' + activity.name
                )
                return JsonResponse({
                    'status': True,
                    'activity': activityDict
                })
        except Exception as ex:
            return JsonResponse({
                'status': False,
                'errMsg': str(ex)
            }, status=403)

    @check_admin_login
    def put(self, request, acid):
        """
        修改活动
        :param request:
        :param acid:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 8
        if adminAuthCheck(admin_user, authID):
            activity = Activity.objects.filter(id=acid)
            if not activity.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '活动不存在'
                }, status=404)
            activity = activity[0]
            pass

        return JsonResponse({
            'status': False,
            'errMsg': '你没有权限操作'
        }, status=401)

    @check_admin_login
    def delete(self, request, acid):
        """
        删除活动
        :param request:
        :param acid:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 6
        if adminAuthCheck(admin_user, authID):
            activity = Activity.objects.filter(id=acid)
            if not activity.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '活动不存在'
                }, status=404)
            activity = activity[0]
            # 关联的任务对象一并删除
            activity.mission.all().delete()
            # 删除活动对象
            activity.delete()
            return JsonResponse({
                'status': True,
                'id': acid
            })
        return JsonResponse({
            'status': True,
            'errMsg': '你没有权限操作'
        }, status=401)
